package utils;

import java.io.Console;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.text.DecimalFormat;

import oshi.SystemInfo;
import oshi.driver.linux.proc.CpuInfo;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;
import view.ServerStateView;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;

public class ServerState {
    SystemInfo si = new SystemInfo();
    OperatingSystem os = si.getOperatingSystem();

    public boolean processExist(long pid)
    {
        OSProcess osprocess = this.os.getProcess((int)pid);
        if (osprocess != null) {
            return true;
        }
        return false;
    }

    public ServerStateView watchSystem()
    {
        HardwareAbstractionLayer hal = si.getHardware();
        GlobalMemory memory = hal.getMemory();

        // 获取内存信息
        long totalMemory = memory.getTotal();
        long availableMemory = memory.getAvailable();
        long usedMemory = totalMemory - availableMemory;
        double memoryUsageRate = (double) usedMemory / totalMemory * 100;

        // 获取 CPU 信息
        CentralProcessor processor = hal.getProcessor();;
        double cpuUsageRate = processor.getSystemCpuLoad(500) * 100;

        ServerStateView serverStateView = new ServerStateView(
            totalMemory / (1024.0 * 1024 * 1024), 
            availableMemory / (1024.0 * 1024 * 1024), 
            usedMemory / (1024.0 * 1024 * 1024),
            memoryUsageRate,
            cpuUsageRate);

        System.out.printf("总内存: %.2f GB%n", serverStateView.totalMemory);
        System.out.printf("可用内存: %.2f GB%n", serverStateView.availableMemory);
        System.out.printf("已用内存: %.2f GB (占用率: %.2f%%)%n", serverStateView.usedMemory, serverStateView.memoryUsageRate);
        System.out.printf("CPU 占用率: %.2f%%%n", serverStateView.cpuUsageRate);

        return serverStateView;
    }

    public Map<String, Double> watchProcess(long pid)
    {
        OSProcess osprocess = this.os.getProcess((int)pid);
        Map<String, Double> res = new HashMap<>();

        if (osprocess != null) {
             // 获取系统内存信息
            GlobalMemory memory = si.getHardware().getMemory();
            // 获取总物理内存
            long totalMemory = memory.getTotal() / (1024 * 1024);
            double cpuUsageRatio = osprocess.getProcessCpuLoadBetweenTicks(osprocess) * 100;
            long memUsageSize = osprocess.getResidentSetSize() / (1024 * 1024);
            double memUsageRatio = ((double)memUsageSize * 100) / totalMemory;
            res.put("cpuUsageRatio", cpuUsageRatio);
            res.put("memUsageSize", Double.valueOf(memUsageSize));
            res.put("memUsageRatio",memUsageRatio);
        } else {
            System.err.println("watchProcess: " + pid + " Process not found.");
        }
        return res;
    }

    public static void main(String[] args) {
        ServerState serverState = new ServerState();
        Map<String, Double> map = serverState.watchProcess(95332);
        System.out.println(map.get("cpuUsageRatio"));
        DecimalFormat df = new DecimalFormat("#");
        System.out.println(df.format(map.get("memUsageSize")));
        System.out.println(map.get("memUsageRatio"));
        serverState.watchSystem();
    }
}
